<template>
  <el-form
    ref="formRef"
    label-width="120px"
    :model="form"
    :rules="formRules"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:40%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item label-width="40%">
      <el-button>取消</el-button>
      <el-button type="primary" @click="doSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSimpleList, addDept, getDeptInfo, deptEditAPI } from '@/api/department'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  // 1.数据
  data() {
    // 定义code校验规则
    const validCode = (rule, value, callback) => {
      // 检查code是否已经存在,如果存在就提示,如果不存在就通过
      let codeList = this.originList.map(item => item.code)
      // 如果是编辑按钮,先要过滤自己
      codeList = this.originList.filter(item => item.pid === this.pid).map(item => item.code)
      codeList.includes(value) ? callback(new Error(value + '已被占用')) : callback()
    }
    // 定义name校验规则
    const validName = (rule, value, callback) => {
      let nameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      if (this.isEdit) {
        const item = this.originList.find(item => item.id === this.pid)
        const pid = item.pid
        nameList = this.originList.filter(item => item.pid !== pid && item.id !== pid).map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error(value + '已被占用')) : callback()
    }
    return {
      // 1.1表单数据
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      // 1.2负责任列表
      list: [],
      // 1.3表单校验数据
      formRules: {
        name: [
          { required: true, message: '请填写部门名称', trigger: 'blur' },
          { min: 2, max: 50, message: '部门名称必须是2-50位', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请填写部门编码', trigger: 'blur' },
          { min: 2, max: 50, message: '部门编码必须是2-50位', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请填写部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请填写部门介绍', trigger: 'blur' },
          { min: 2, max: 300, message: '部门介绍必须是2-300位', trigger: 'blur' }
        ]

      }
    }
  },
  // 2.created
  created() {
    // 2.1获取下拉框的数据
    this.loadSimple()
    if (this.isEdit) {
      this.getDeptInfo()
    }
  },
  // 3.方法
  methods: {
    // 3.1获取员工简单列表
    async loadSimple() {
      const res = await getSimpleList()
      this.list = res.data
    },
    // 3.2点击确认按钮
    async doSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          // 判断是添加还是新增
          this.isEdit ? this.deptEdit() : this.deptAdd()
        }
      })
    },
    // 3.3获取当前点击部门的信息
    async getDeptInfo() {
      const res = await getDeptInfo(this.pid)
      this.form = res.data
    },
    // 3.4添加部门
    async deptAdd() {
      try {
        await addDept({ ...this.form, pid: this.pid })
        // 通知父组件关闭弹框+刷新列表
        this.$emit('reset')
        this.$message.success('添加成功')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 3.5编辑部门
    async deptEdit() {
      await deptEditAPI(this.form)
      // 通知父组件关闭弹窗+刷新数据
      this.$emit('editDone')
      this.$message.success('编辑成功')
    }
  }
}
</script>

<style>

</style>
